using System.Collections;
using UnityEngine;

public class GateHealthUI : MonoBehaviour
{
	[SerializeField]
	private UISprite healthBorder;

	[SerializeField]
	private UISprite healthSprite;

	[SerializeField]
	private UISprite imageAlive;

	[SerializeField]
	private UISprite imageDestroyed;

	[SerializeField]
	private UISprite hitImage;

	private float maxHealth;

	private int maxWidth;

	public UILabel textOverImage;

	private void Awake()
	{
		maxWidth = healthSprite.width;
	}

	public void Init(float _maxHealth)
	{
		maxHealth = _maxHealth;
		healthSprite.width = maxWidth;
	}

	public void SetHealthUI(float health)
	{
		if (health > 0f)
		{
			SetGateAlive(health);
		}
	}

	public void SetGateDestroyedUI()
	{
		SetGateDestroyed();
	}

	private void SetGateAlive(float health)
	{
		imageAlive.gameObject.SetActive(true);
		imageDestroyed.gameObject.SetActive(false);
		textOverImage.gameObject.SetActive(true);
		if (maxHealth != 0f)
		{
			int width = healthSprite.width;
			int num = Mathf.CeilToInt(health / maxHealth * (float)maxWidth);
			if (health <= maxHealth)
			{
				healthSprite.fillAmount = health / maxHealth;
			}
		}
	}

	public void ShowHit()
	{
		CopsAndRobbersInterface.instance.StartCoroutine(ShowHitCoroutine());
	}

	private IEnumerator ShowHitCoroutine()
	{
		hitImage.gameObject.SetActive(true);
		yield return new WaitForSeconds(0.23f);
		hitImage.gameObject.SetActive(false);
	}

	private void SetGateDestroyed()
	{
		imageDestroyed.gameObject.SetActive(true);
		textOverImage.gameObject.SetActive(false);
		healthSprite.width = 1;
	}

	public void SetGateSemitransparent(bool setTransparent)
	{
		float alpha = ((!setTransparent) ? 1f : 0.5f);
		healthBorder.alpha = alpha;
		healthSprite.alpha = alpha;
		imageAlive.alpha = alpha;
		imageDestroyed.alpha = alpha;
		textOverImage.alpha = alpha;
	}
}
